/*
 * RBFCommissioning.c
 *
 *  Created on: 2023年7月12日
 *      Author: fjx
 */

#include "RBFCommon.h"
#include "RBFCommissioning.h"
#include "app_log.h"
#include "bsp_sha.h"
#include "RBFKeyNvm.h"
#include <string.h>
#include "util.h"
#include "device.h"
#include "net_addressing.h"
#include "app_api.h"

#define xCONFIG_LOG_RBFCOMMISSIONING                                         //模块日志宏定义,使能,模块输出日志

#if defined (CONFIG_LOG_RBFCOMMISSIONING)
#define CONFIG_DBG_CHECK_PRO
#if defined (CONFIG_DBG_CHECK_PRO)//验证协议正确与否
RBFMacHeader_t set_header;
RBFMacMessageJoinRequest_t set_msg_join_request;
RBFMacMessageNodeJoinConfirm_t set_msg_join_confirm;
RBFMacMessageGWAlloawJoinNetwork_t set_msg_gw_allow_join_net;
#endif
#endif
/*
 * 数据获取和数据设置接口
 *
 */
#if defined (CONFIG_LOG_RBFCOMMISSIONING)
static char *get_status_str(RBFCommissioningStatus_t status)
{
  switch (status) {
  case RBF_COMMSISSIONING_NODE_PAGING_HDR_PARSE_TRUE:
    return "RBF_COMMSISSIONING_NODE_PAGING_HDR_PARSE_TRUE";
  case RBF_COMMSISSIONING_NODE_PAGING_HDR_PARSE_FALSE:
    return "RBF_COMMSISSIONING_NODE_PAGING_HDR_PARSE_FALSE";
  case RBF_COMMSISSIONING_NODE_PAGING_PYD_PARSE_TRUE:
    return "RBF_COMMSISSIONING_NODE_PAGING_PYD_PARSE_TRUE";
  case RBF_COMMSISSIONING_NODE_PAGING_PYD_PARSE_FALSE:
    return "RBF_COMMSISSIONING_NODE_PAGING_PYD_PARSE_FALSE";
  case RBF_COMMSISSIONING_NODE_PAGING_MSG_LEN_ERROR:
    return "RBF_COMMSISSIONING_NODE_PAGING_MSG_LEN_ERROR";
  case RBF_COMMSISSIONING_NODE_PAGING_CMD_ERROR:
    return "RBF_COMMSISSIONING_NODE_PAGING_CMD_ERROR";


  case RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_HDR_PARSE_TRUE:
    return "RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_HDR_PARSE_TRUE";
  case RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_HDR_PARSE_FALSE:
    return "RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_HDR_PARSE_FALSE";
  case RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_PYD_PARSE_TRUE:
    return "RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_PYD_PARSE_TRUE";
  case RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_PYD_PARSE_FALSE:
    return "RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_PYD_PARSE_FALSE";
  case RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_MSG_LEN_ERROR:
    return "RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_MSG_LEN_ERROR";
  case RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_CMD_ERROR:
    return "RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_CMD_ERROR";

  case RBF_COMMSISSIONING_GW_ALLOW_JOIN_HRD_PARSE_TRUE:
    return "RBF_COMMSISSIONING_GW_ALLOW_JOIN_HRD_PARSE_TRUE";
  case RBF_COMMSISSIONING_GW_ALLOW_JOIN_HRD_PARSE_FALSE:
    return "RBF_COMMSISSIONING_GW_ALLOW_JOIN_HRD_PARSE_FALSE";
  case RBF_COMMSISSIONING_GW_ALLOW_JOIN_PYD_PARSE_TRUE:
    return "RBF_COMMSISSIONING_GW_ALLOW_JOIN_PYD_PARSE_TRUE";
  case RBF_COMMSISSIONING_GW_ALLOW_JOIN_PYD_PARSE_FALSE:
    return "RBF_COMMSISSIONING_GW_ALLOW_JOIN_PYD_PARSE_FALSE";
  case RBF_COMMSISSIONING_GW_ALLOW_JOIN_MSG_LEN_ERROR:
    return "RBF_COMMSISSIONING_GW_ALLOW_JOIN_MSG_LEN_ERROR";
  case RBF_COMMSISSIONING_GW_ALLOW_JOIN_CMD_ERROR:
    return "RBF_COMMSISSIONING_GW_ALLOW_JOIN_CMD_ERROR";

  default:
    return "Unknown status";
  }
}
#endif


/*
 *   获取m值
 *   hash256(SN),取低16Byte
 *
 *
 */
void RBF_get_m (uint64_t sn,uint8_t * m)
{
  uint8_t m_buff[32];
  uint8_t sn_buff[8];
  size_t hash_len;

  l_put_be64(sn,sn_buff);
  DS_hash_compute(HASH_SHA2_256,sn_buff, sizeof(sn_buff),
                           m_buff, sizeof(m_buff),
                           &hash_len);

  memcpy(m,m_buff,16);

}
#if defined (CONFIG_LOG_RBFCOMMISSIONING)
/*
 * 节点寻呼payload log
 *
 */
void RBF_node_paging_payload_log (RBFMacMessageJoinRequest_t msg)
{

  DS_app_log_debug("macAdd2 = 0x%0x\n",msg.macAdd2);
  DS_app_log_debug("msgSeq = 0x%0x\n",msg.msgSeq);
  DS_app_log_debug("msgRetry = 0x%0x\n",msg.msgRetry);
  DS_app_log_debug("battV = 0x%0x\n",msg.battV);
  DS_app_log_debug("rssi = 0x%0x\n",msg.rssi);
  DS_app_log_debug("temp = 0x%0x\n",msg.temp);
  DS_app_log_debug("qrfR1 = 0x%0x\n",msg.qrfR1);
  DS_app_log_debug("qrfR2 = 0x%0x\n",msg.qrfR2);
  DS_app_log_debug("echASeq = 0x%0x\n",msg.echASeq);
  DS_app_log_debug("deviceType = 0x%0x\n",msg.deviceType);
  DS_app_log_debug("modelType = 0x%0x\n",msg.modelType);
  DS_app_log_debug("hardType = 0x%0x\n",msg.hardType);
  DS_app_log_debug("macAdd8 = 0x%0llx\n",msg.macAdd8); //
  DS_app_log_debug("rfVer = 0x%0x\n",msg.rfVer);
  DS_app_log_debug("sVer = 0x%0x\n",msg.sVer);
  DS_app_log_hexdump_debug("keyCodeA = ",msg.keyCodeA,RBF_KEY_LENGTH_MAX);
  DS_app_log_debug("mac = 0x%lx\n",msg.mac);



}
/*
 * 节点入网确认payload log
 *
 */
void RBF_node_join_confirm_payload_log (RBFMacMessageNodeJoinConfirm_t msg)
{

  DS_app_log_debug("macAdd2 = 0x%0x\n",msg.macAdd2);
  DS_app_log_debug("msgSeq = 0x%0x\n",msg.msgSeq);
  DS_app_log_debug("msgRetry = 0x%0x\n",msg.msgRetry);
  DS_app_log_debug("battV = 0x%0x\n",msg.battV);
  DS_app_log_debug("rssi = 0x%0x\n",msg.rssi);
  DS_app_log_debug("temp = 0x%0x\n",msg.temp);
  DS_app_log_debug("qrfR1 = 0x%0x\n",msg.qrfR1);
  DS_app_log_debug("qrfR2 = 0x%0x\n",msg.qrfR2);
  DS_app_log_debug("echASeq = 0x%0x\n",msg.echASeq);
  DS_app_log_debug("deviceType = 0x%0x\n",msg.deviceType);
  DS_app_log_debug("modelType = 0x%0x\n",msg.modelType);
  DS_app_log_debug("hardType = 0x%0x\n",msg.hardType);
  DS_app_log_debug("macAdd8 = 0x%0llx\n",msg.macAdd8); //
  DS_app_log_debug("rfVer = 0x%0x\n",msg.rfVer);
  DS_app_log_debug("sVer = 0x%0x\n",msg.sVer);

  DS_app_log_debug("mac = 0x%lx\n",msg.mac);



}
/*
 *
 */
void RBF_gw_allow_join_net_payload_log (RBFMacMessageGWAlloawJoinNetwork_t msg)
{

  DS_app_log_debug("macAdd4 = 0x%0lx\n",msg.macAdd4);
  DS_app_log_debug("rollingCode = 0x%0lx\n",msg.rollingCode);
  DS_app_log_debug("systemID = 0x%0x\n",msg.systemID);
  DS_app_log_debug("localID = 0x%0x\n",msg.localID);

  DS_app_log_hexdump_debug("keyCodeB = ",msg.keyCodeB,RBF_KEY_LENGTH_MAX);
  DS_app_log_hexdump_debug("systemKeyCode = ",msg.systemKeyCode,RBF_KEY_LENGTH_MAX);
  DS_app_log_hexdump_debug("broadcastKeyCode = ",msg.broadcastKeyCode,RBF_KEY_LENGTH_MAX);

  DS_app_log_debug("mac = 0x%0lx\n",msg.mac);

}
#endif
/*
 * 网关允许节点入网payload解包
 *
 */
void RBF_gw_allow_join_net_payload_packetToMsg(uint8_t *packet,RBFMacMessageGWAlloawJoinNetwork_t *msg)
{
  msg->macAdd4 = l_get_be32(packet+RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_MACADD4);
  msg->rollingCode = l_get_be32(packet+RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_ROLLINGCODE);
#if 0
  msg->systemID = (l_get_be32(packet+RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_SYSTEMID)>>8)&0x00FFFFFF;  //be32(systemID[24]+locaID[8]
#else
  msg->systemID = l_get_be24(packet+RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_SYSTEMID);
#endif
  msg->localID = l_get_be16(packet+RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_LOCALID);

  memcpy(msg->keyCodeB,&packet[RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_KEYCODEB],RBF_KEY_LENGTH_MAX);
  memcpy(msg->systemKeyCode,&packet[RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_SYSTEMKEYCODE],RBF_KEY_LENGTH_MAX);
  memcpy(msg->broadcastKeyCode,&packet[RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_BROADCASTKEYCODE],RBF_KEY_LENGTH_MAX);

  msg->mac = l_get_be32(packet+RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_MAC);


}
/*
 * 网关允许节点入网payload组包
 *
 *
 */
void RBF_gw_allow_join_net_payload_msgToPacket(RBFMacMessageGWAlloawJoinNetwork_t msg, uint8_t *packet)
{
  l_put_u8(RBF_MAC_PACKET_GW_ALLOW_JOIN_NETWORK_LEN,packet+RBF_MAC_PACKET_LEN_INDEX); //packet length

  l_put_be32(msg.macAdd4,packet+RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_MACADD4);
  l_put_be32(msg.rollingCode,packet+RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_ROLLINGCODE);
#if 0
  l_put_be32(((uint32_t)msg.systemID)<<8,packet+RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_SYSTEMID);
#else
  l_put_be24(msg.systemID,packet+RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_SYSTEMID);
#endif
  l_put_be16(msg.localID,packet+RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_LOCALID);

  memcpy(&packet[RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_KEYCODEB],msg.keyCodeB,RBF_KEY_LENGTH_MAX);
  memcpy(&packet[RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_SYSTEMKEYCODE],msg.systemKeyCode,RBF_KEY_LENGTH_MAX);
  memcpy(&packet[RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_BROADCASTKEYCODE],msg.broadcastKeyCode,RBF_KEY_LENGTH_MAX);
  l_put_be32(msg.mac,packet+RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_MAC);

}

/*
 *
 *  网关允许节点入网
 *
 */
RBF_RET_STATUS RBF_get_gw_allow_join_net(uint8_t *packet,uint16_t packet_len)
{
  RBFMacHeader_t hdr;




  if(packet_len < RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NETWORK_LEN){

#if defined (CONFIG_LOG_RBFCOMMISSIONING)
      DS_app_log_info("===%s [error len=%d]===\n",
             get_status_str(RBF_COMMSISSIONING_GW_ALLOW_JOIN_MSG_LEN_ERROR),
             packet_len);
#endif
      return RBF_RET_STATUS_LEN_ERROR;
  }
  if(packet[RBF_MAC_HEADER_INDEX_CMD] != RBF_CMD_GATEWAY_ALLOW_JOIN_NETWORK){

#if defined (CONFIG_LOG_RBFCOMMISSIONING)
      DS_app_log_info("===%s [error cmd=%d] ===\n",
             get_status_str(RBF_COMMSISSIONING_GW_ALLOW_JOIN_CMD_ERROR),
             packet[RBF_MAC_HEADER_INDEX_CMD]);
#endif
      return RBF_RET_STATUS_CMD_ERROR;
  }

  //1. hdr parse
#if defined (CONFIG_LOG_RBFCOMMISSIONING)
  DS_app_log_info("===[get]GW allow join net header parse:===\n");
#endif
  RBF_header_parse(packet,RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX, &hdr);
  //2. 解析网络允许入网
  RBFMacMessageGWAlloawJoinNetwork_t msg;
  RBF_gw_allow_join_net_payload_packetToMsg(packet,&msg);

  if(msg.macAdd4 !=  getMacAdd4()) {
      DS_app_log_error("***Mac address diff!***\n");
      DS_app_log_error("RX mac4:0x%0lx, Node mac4 0x%0lx\n",msg.macAdd4,getMacAdd4());
      return RBF_RET_STATUS_FAIL;
  }

  //保存RfmID
  setThisParam(ENpbyRfmId,&hdr.destID);


#if defined (CONFIG_DBG_CHECK_PRO)
  DS_app_log_info("===GW allow join net parse:===\n");
  //比对数据
  //1.比对header
  if( memcmp(&hdr,&set_header,sizeof(RBFMacHeader_t)) == 0){
      DS_app_log_info("===%s===\n",get_status_str(RBF_COMMSISSIONING_GW_ALLOW_JOIN_HRD_PARSE_TRUE));
  }
  else {
      DS_app_log_info("===%s===\n",get_status_str(RBF_COMMSISSIONING_GW_ALLOW_JOIN_HRD_PARSE_FALSE));
      DS_app_log_hexdump_error("[set]:",(uint8_t *)&set_header,sizeof(RBFMacHeader_t));
      DS_app_log_hexdump_error("[get]:",(uint8_t *)&hdr,sizeof(RBFMacHeader_t));


  }
  //2.比对payload
  /*
   *  数据真实性比较不包含CCM值
   */
  if(memcmp(&set_msg_gw_allow_join_net,&msg,sizeof(RBFMacMessageGWAlloawJoinNetwork_t)-4) == 0){//
      DS_app_log_info("===%s===\n",get_status_str(RBF_COMMSISSIONING_GW_ALLOW_JOIN_PYD_PARSE_TRUE));
  }
  else {
      DS_app_log_info("===%s===\n",get_status_str(RBF_COMMSISSIONING_GW_ALLOW_JOIN_PYD_PARSE_FALSE));
      RBF_gw_allow_join_net_payload_log(set_msg_gw_allow_join_net);
      RBF_gw_allow_join_net_payload_log(msg);

  }
#endif


  return RBF_RET_STATUS_SUCCESS;
}
/*
 * 网关允许节点入网
 *
 */
void RBF_set_gw_allow_join_net(RBFMacHeader_t hdr,RBFMacMessageGWAlloawJoinNetwork_t msg,uint8_t * packet)
{
//  uint8_t ret;
/*
 * 1。 header 组包
 * 2. payload组包
 * 3. 待加密
 *
 */

 // RBFMacHeader_t hdr;
//  uint8_t packet[RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NETWORK_LEN]={0};
//  uint8_t packet_len;

  hdr.rollungCode = 0x12345678;    //4Bytes
  hdr.frameCtl = RBF_VERSION;       //1Byte


  hdr.syncClock  = 0xaabb;      //2Bytes
  hdr.hsclock = 0xcc;        //1Byte
  hdr.panId = RBF_DEFAULT_PANID;          //3Bytes
  hdr.srcId = RBF_DEFAULT_SRCID;          //1Byte
  //hdr.destID = RBF_DEFAULT_DESTID;         //1Byte
  hdr.routID = RBF_DEFAULT_ROUTEID;         //1Byte
  hdr.cmd = RBF_CMD_GATEWAY_ALLOW_JOIN_NETWORK;            //1Byte
#if defined (CONFIG_DBG_CHECK_PRO)
  //copy
  memcpy(&set_msg_gw_allow_join_net,&msg,sizeof(RBFMacMessageGWAlloawJoinNetwork_t));
  memcpy(&set_header,&hdr,sizeof(RBFMacHeader_t));
#endif
//1.
  RBF_packet_build_header(hdr,packet);      //header组包

#if defined (CONFIG_LOG_RBFCOMMISSIONING)
  DS_app_log_hexdump_debug("gw allow join header: ",packet+RBF_MAC_PACKET_LEN_SIZE,RBF_HEADER_LENGTH_MAX);//不包含len

  //2.payload
  DS_app_log_info("===[set]gw allow join origin:===\n");
  RBF_gw_allow_join_net_payload_log(msg);
#endif

  //Build packet  TODO:明文(为加密)
  RBF_gw_allow_join_net_payload_msgToPacket(msg,packet);

}


void RBF_node_paging_payload_packetToMsg(uint8_t *packet,RBFMacMessageJoinRequest_t *msg)
{
  uint8_t key[16];
  uint8_t m[32];


  msg->macAdd2 = l_get_be16(packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2);//15

  msg->msgSeq = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MSGSEQ); //17
  msg->msgRetry = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MSGRETRY);//18
  msg->battV = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_BATTV);//19
  msg->rssi = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_RSSI);//20
  msg->temp = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_TEMP);//21
  msg->qrfR1 = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_QRFR1);//22
  msg->qrfR2 = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_QRF2);//23
  msg->echASeq = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_ECHASEQ);//24

  msg->deviceType = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_DEVICETYPE);//25
  msg->modelType = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MODELTYPE);//26
  msg->hardType = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_HARDTYPE);//27
  msg->macAdd8 = l_get_be64(packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD8);//28
#if 0
  msg->rfVer = (l_get_be32(packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_RFVER)>>8)&0x00FFFFFF;// rfVer(3B)+sVer(1B)
  msg->sVer  = (l_get_be32(packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_RFVER)&0xFF);
#else
  msg->rfVer = l_get_be24(packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_RFVER);// rfVer(3B)
  msg->sVer  = l_get_be24(packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_SVER);// sVer(3B)
//  DS_app_log_error("Get App:%x.%x.%x\n",(msg->sVer>>16)&0xff,(msg->sVer>>8)&0xff,(msg->sVer)&0xff);

#endif
  //msg->rfVer = (msg->rfVer>>8)&0x00FFFFFF;
  memcpy(msg->keyCodeA,&packet[RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_KEYCODEA],RBF_KEY_LENGTH_MAX);
//  memcpy(codeA,&packet[RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_KEYCODEA],RBF_KEY_LENGTH_MAX);//40

  msg->mac = l_get_be32(packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MAC);
  /*计算codeAkey
    通过hash(sn)得出m,再key^m计算出真正的keycodeA

  */
  memset(key,0,RBF_KEY_LENGTH_MAX);
  memcpy(key,&packet[RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_KEYCODEA],RBF_KEY_LENGTH_MAX); //40
  RBF_get_m(msg->macAdd8,m);
  XorDataLine(key,m,RBF_KEY_LENGTH_MAX);
  memcpy(msg->keyCodeA,key,RBF_KEY_LENGTH_MAX);
 // memcpy(codeA,key,RBF_KEY_LENGTH_MAX);
  memcpy(msg->sn, &packet[RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_SN],SN_LENGTH);
}
/*
 * 节点寻呼payload组包
 *
 *
 */
void RBF_node_paging_payload_msgToPacket(RBFMacMessageJoinRequest_t msg, uint8_t *packet)
{
  uint8_t m[32];
  uint8_t codeA[16]={0};

  memcpy(codeA,msg.keyCodeA,RBF_KEY_LENGTH_MAX);

  RBF_get_m(msg.macAdd8,m);
  XorDataLine(codeA,m,RBF_KEY_LENGTH_MAX);

#if defined (CONFIG_LOG_RBFCOMMISSIONING)
  DS_app_log_hexdump_debug("origin CodeA: ",msg.keyCodeA,sizeof(msg.keyCodeA));
  DS_app_log_hexdump_debug("Xor codeA:    ",codeA,sizeof(codeA));

#endif
  l_put_u8(RBF_MAC_PACKET_JOIN_REQ_LEN,packet+RBF_MAC_PACKET_LEN_INDEX); //packet length


  //Build packet
  l_put_be16(msg.macAdd2,packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD2); //15
  l_put_u8(msg.msgSeq,packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MSGSEQ); //17
  l_put_u8(msg.msgRetry,packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MSGRETRY); //18
  l_put_u8(msg.battV,packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_BATTV); //19
  l_put_u8(msg.rssi,packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_RSSI); //20
  l_put_u8(msg.temp,packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_TEMP); //21
  l_put_u8(msg.qrfR1,packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_QRFR1); //22
  l_put_u8(msg.qrfR2,packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_QRF2); //23
  l_put_u8(msg.echASeq,packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_ECHASEQ); //24

  l_put_u8(msg.deviceType,packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_DEVICETYPE); //25
  l_put_u8(msg.modelType,packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MODELTYPE); //26
  l_put_u8(msg.hardType,packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_HARDTYPE); //27
  l_put_be64(msg.macAdd8,packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MACADD8);//28
#if 0
  l_put_be32((((uint32_t)msg.rfVer<<8)&0xFFFFFF00) | msg.sVer,packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_RFVER);// rfVer(3B)+sVer(1B)
#else
  l_put_be24(msg.rfVer,packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_RFVER);// rfVer(3B)
  l_put_be24(msg.sVer,packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_SVER); //sVer(3B)
//  DS_app_log_error("Get App:%x.%x.%x\n",(msg.sVer>>16)&0xff,(msg.sVer>>8)&0xff,(msg.sVer)&0xff);
#endif

  memcpy(&packet[RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_KEYCODEA],codeA,RBF_KEY_LENGTH_MAX); //40
  memcpy(&packet[RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_SN],msg.sn,SN_LENGTH);

  /*
   * MAC:
   * TODO: 针对节点寻呼功能,mac应该如何处理. 用hash算法
   */

  l_put_be32(msg.mac,packet+RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_MAC);//RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_JOIN_REQ_LEN-4);

#if defined (CONFIG_LOG_RBFCOMMISSIONING)
  DS_app_log_info("[MAC]len+ Join header + PDU (len=%d)\n",
         RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_JOIN_REQ_LEN);
  DS_app_log_hexdump_debug("node paging:",packet,RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_JOIN_REQ_LEN);

#endif
}


//
/*
 *  节点寻呼功能
 *
 */
void RBF_set_node_paging(RBFMacMessageJoinRequest_t msg,uint8_t *packet)
{



  RBFMacHeader_t hdr;
//  uint8_t packet[RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_JOIN_REQ_LEN]={0};
//  uint8_t packet_len;

//  hdr.rollungCode = 0x12345678;    //4Bytes
  hdr.frameCtl = RBF_VERSION;       //1Byte


  hdr.syncClock  = 0xaabb;      //2Bytes
  hdr.hsclock = 0xcc;        //1Byte
  hdr.panId = RBF_DEFAULT_PANID;          //3Bytes
  hdr.srcId = RBF_DEFAULT_SRCID;          //1Byte
  hdr.destID = RBF_HUB_ID;                //1Byte
  hdr.routID = RBF_DEFAULT_ROUTEID;         //1Byte
  hdr.cmd = RBF_CMD_NODE_PAGING;            //1Byte
#if defined (CONFIG_DBG_CHECK_PRO)
  //copy
  memcpy(&set_msg_join_request,&msg,sizeof(RBFMacMessageJoinRequest_t));
  memcpy(&set_header,&hdr,sizeof(RBFMacHeader_t));
#endif

  RBF_packet_build_header(hdr,packet);      //header组包


#if defined (CONFIG_LOG_RBFCOMMISSIONING)
  DS_app_log_hexdump_debug("node paging header: ",packet+RBF_MAC_HEADER_INDEX_FIRST,RBF_HEADER_LENGTH_MAX);//不打印数据包长度值



  //====payload
  DS_app_log_info("===[set]node paging origin:===\n");
  RBF_node_paging_payload_log(msg);
#endif
  RBF_node_paging_payload_msgToPacket(msg, packet);


}

/*
 *  节点寻呼数据解析
 *@param packet 待解析的数据
 *@param packet_len 待解析的数据长度
 *@param
 *
 */
RBF_RET_STATUS RBF_get_node_paging_packet_parse(uint8_t *packet,uint16_t packet_len,RBFMacMessageJoinRequest_t *msg)//;uint8_t *codeA,uint8_t *macAdd8)
{
  RBFMacHeader_t hdr;
//  uint8_t key[16];
//  uint8_t m[32];
//  uint8_t buff[8];


  if(packet_len < RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_JOIN_REQ_LEN){
#if defined (CONFIG_LOG_RBFCOMMISSIONING)
      DS_app_log_info("===%s [error len=%d]===\n",
             get_status_str(RBF_COMMSISSIONING_NODE_PAGING_MSG_LEN_ERROR),
             packet_len);
#endif
      return RBF_RET_STATUS_LEN_ERROR;
  }
  if(packet[RBF_MAC_HEADER_INDEX_CMD] != RBF_CMD_NODE_PAGING){
#if defined (CONFIG_LOG_RBFCOMMISSIONING)
      DS_app_log_info("===%s [error cmd=%d] ===\n",
             get_status_str(RBF_COMMSISSIONING_NODE_PAGING_CMD_ERROR),
             packet[RBF_MAC_HEADER_INDEX_CMD]);
#endif
      return RBF_RET_STATUS_CMD_ERROR;
  }

  //hdr parse
#if defined (CONFIG_LOG_RBFCOMMISSIONING)
  DS_app_log_info("===[get]node paging header parse:===\n");
#endif
  RBF_header_parse(packet,RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX, &hdr);

//  RBFMacMessageJoinRequest_t msg;
  RBF_node_paging_payload_packetToMsg(packet,msg);
//  memcpy(codeA,msg->keyCodeA,RBF_KEY_LENGTH_MAX);
// //rfm mac
//  l_put_be64(msg->macAdd8,macAdd8);


#if defined (CONFIG_DBG_CHECK_PRO)
  DS_app_log_info("===node paging parse:===\n");
  //比对数据
  //1.比对header
  if( memcmp(&hdr,&set_header,sizeof(RBFMacHeader_t)) == 0){
      DS_app_log_info("===%s===\n",get_status_str(RBF_COMMSISSIONING_NODE_PAGING_HDR_PARSE_TRUE));
  }
  else {
      DS_app_log_info("===%s===\n",get_status_str(RBF_COMMSISSIONING_NODE_PAGING_HDR_PARSE_FALSE));
      DS_app_log_hexdump_error("[set]:",(uint8_t *)&set_header,sizeof(RBFMacHeader_t));
      DS_app_log_hexdump_error("[get]:",(uint8_t *)&hdr,sizeof(RBFMacHeader_t));


  }
  //2.比对payload
  if(memcmp(&set_msg_join_request,msg,sizeof(RBFMacMessageJoinRequest_t)-4) == 0){
      DS_app_log_info("===%s===\n",get_status_str(RBF_COMMSISSIONING_NODE_PAGING_PYD_PARSE_TRUE));
  }
  else {
      DS_app_log_info("===%s===\n",get_status_str(RBF_COMMSISSIONING_NODE_PAGING_PYD_PARSE_FALSE));
      RBF_node_paging_payload_log(set_msg_join_request);
      RBF_node_paging_payload_log(*msg);

  }
#endif


  return RBF_RET_STATUS_SUCCESS;
}

//====
void RBF_node_join_confirm_payload_packetToMsg(uint8_t *packet,RBFMacMessageNodeJoinConfirm_t *msg)
{
  uint8_t m[32];

  msg->msgSeq = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MEGSEQ);
  msg->msgRetry = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MSGRETRY);
  msg->battV = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_BATTV);
  msg->rssi = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_RSSI);
  msg->temp = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_TEMP);
  msg->macAdd2 = l_get_be16(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MACADD2);
  msg->qrfR1 = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_QRFR1);
  msg->qrfR2 = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_QFRR2);
  msg->echASeq = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_ECHASEQ);

  msg->deviceType = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_DEVICETYPE);
  msg->modelType = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MODELTYPE);
  msg->hardType = l_get_u8(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_HARDTYPE);
  msg->macAdd8 = l_get_be64(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MACADD8);

#if 0
  msg->rfVer = (l_get_be32(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_RFVER)>>8)&0x00FFFFFF;// rfVer(3B)+sVer(1B)
  msg->sVer  = (l_get_be32(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_RFVER)&0xFF);
#else
  msg->rfVer = l_get_be24(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_RFVER);// rfVer(3B)
  msg->sVer  = l_get_be24(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_SVER);// sVer(3B)
//  DS_app_log_error("Get App:%x.%x.%x\n",(msg->sVer>>16)&0xff,(msg->sVer>>8)&0xff,(msg->sVer)&0xff);
#endif
  //msg->rfVer = (msg->rfVer>>8)&0x00FFFFFF;

  memcpy(msg->sn,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_SN,SN_LENGTH);
  msg->mac = l_get_be32(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MAC);
  /*计算codeAkey
    通过hash(sn)得出m,再key^m计算出真正的keycodeA

  */

  RBF_get_m(msg->macAdd8,m);      //计算sn




}
/*
 * 网关允许节点入网payload组包
 *
 *
 */
void RBF_node_join_confirm_payload_msgToPacket(RBFMacMessageNodeJoinConfirm_t msg, uint8_t *packet)
{
  uint8_t m[32];
  uint8_t data[64]={0};
  uint8_t len=0;

  RBF_get_m(msg.macAdd8,m);          //获取m值，需要更改aes(defaultkey1)
  len = get_device_state(data);
#if defined (JOIN_CONFIRM_WITH_KEYVALUE)
  l_put_u8(RBF_MAC_PACKET_JOIN_CONFIRM_LEN+len,packet+RBF_MAC_PACKET_LEN_INDEX); //packet length
#else
  l_put_u8(RBF_MAC_PACKET_JOIN_CONFIRM_LEN,packet+RBF_MAC_PACKET_LEN_INDEX); //packet length
#endif

  //Build packet
  //
  l_put_u8(msg.msgSeq,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MEGSEQ);//15
  l_put_u8(msg.msgRetry,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MSGRETRY);//16
  l_put_u8(msg.battV,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_BATTV);//17
  l_put_u8(msg.rssi,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_RSSI);//18
  l_put_u8(msg.temp,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_TEMP);//19
  l_put_be16(msg.macAdd2,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MACADD2);//20
  l_put_u8(msg.qrfR1,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_QRFR1);//22
  l_put_u8(msg.qrfR2,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_QFRR2);//23
  l_put_u8(msg.echASeq,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_ECHASEQ);//24

  l_put_u8(msg.deviceType,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_DEVICETYPE);//25
  l_put_u8(msg.modelType,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MODELTYPE);//26
  l_put_u8(msg.hardType,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_HARDTYPE);//27
  l_put_be64(msg.macAdd8,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MACADD8);//28


#if 0
  l_put_be32((((uint32_t)msg.rfVer<<8)&0xFFFFFF00) | msg.sVer,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_RFVER);//36 rfVer(3B)+sVer(1B)
#else
  l_put_be24( msg.rfVer,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_RFVER);//rfVer(3B)
  l_put_be24( msg.sVer,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_SVER);//sVer(3B)
//  DS_app_log_error("Get App:%x.%x.%x\n",(msg.sVer>>16)&0xff,(msg.sVer>>8)&0xff,(msg.sVer)&0xff);
#endif

#if defined (JOIN_CONFIRM_WITH_KEYVALUE)
  memcpy(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MAC,data,len);
#endif
  memcpy(packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_SN,msg.sn,SN_LENGTH);
  /*
   * MAC:
   * TODO: 针对节点寻呼功能,mac应该如何处理. 用hash算法
   */
#if defined (JOIN_CONFIRM_WITH_KEYVALUE)
  l_put_be32(msg.mac,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MAC+len);
#else
  l_put_be32(msg.mac,packet+RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_MAC);//40;
#endif

#if defined (CONFIG_LOG_RBFCOMMISSIONING)
  DS_app_log_info("[MAC]len+ Join header + PDU (len=%d) \n",
         RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_JOIN_CONFIRM_LEN);
  DS_app_log_hexdump_debug("node join confirm:",packet,RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_JOIN_CONFIRM_LEN);


#endif
}
//===
/*
 * 节点入网确认
 *
 */
void RBF_set_node_join_confirm(RBFMacMessageNodeJoinConfirm_t msg,uint8_t *packet)
{
  RBFMacHeader_t hdr;
//  uint8_t packet[RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_JOIN_REQ_LEN]={0};
//  uint8_t packet_len;

#if 1  //header 测试数据
  hdr.rollungCode = 0x12345678;    //4Bytes
  hdr.frameCtl = RBF_VERSION;       //1Byte


  hdr.syncClock  = 0xaabb;      //2Bytes
  hdr.hsclock = 0xcc;        //1Byte
#if 0
  hdr.panId = RBF_DEFAULT_PANID;
#else
  hdr.panId = readThisSystemId(); //修改为读取systemID          //3Bytes
#endif
  //hdr.srcId = RBF_DEFAULT_SRCID;          //1Byte
  hdr.destID = 0;//RBF_DEFAULT_DESTID;         //1Byte
  hdr.routID = 0;//RBF_DEFAULT_ROUTEID;         //1Byte
  hdr.cmd = RBF_CMD_NODE_JOIN_CONFIRM;      //1Byte
#endif
  getThisParam(ENpbyRfmId,&hdr.srcId);
#if defined (CONFIG_DBG_CHECK_PRO)
  //copy
  memcpy(&set_msg_join_confirm,&msg,sizeof(RBFMacMessageNodeJoinConfirm_t));
  memcpy(&set_header,&hdr,sizeof(RBFMacHeader_t));
#endif
  //1.header
  RBF_packet_build_header(hdr,packet);      //header组包


#if defined (CONFIG_LOG_RBFCOMMISSIONING)
  DS_app_log_hexdump_debug("node join confirm header:0x[",packet+RBF_MAC_HEADER_INDEX_FIRST,RBF_HEADER_LENGTH_MAX);


  //2.payload
  DS_app_log_info("===[set]node join confirm origin:===\n");
  RBF_node_join_confirm_payload_log(msg);
#endif
  RBF_node_join_confirm_payload_msgToPacket(msg,packet);


}

/*
 *  节点寻呼数据解析
 *@param packet 待解析的数据
 *@param packet_len 待解析的数据长度
 *@param
 *
 */
RBF_RET_STATUS RBF_get_node_join_confirm(uint8_t *packet,uint16_t packet_len,RBFMacMessageNodeJoinConfirm_t *msg)
{
  RBFMacHeader_t hdr;



  if(packet_len < RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_JOIN_CONFIRM_LEN){
#if defined (CONFIG_LOG_RBFCOMMISSIONING)
      DS_app_log_info("===%s [error len=%d]===\n",
             get_status_str(RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_MSG_LEN_ERROR),
             packet_len);
#endif
      return RBF_RET_STATUS_LEN_ERROR;
  }
  if(packet[RBF_MAC_HEADER_INDEX_CMD] != RBF_CMD_NODE_JOIN_CONFIRM){
#if defined (CONFIG_LOG_RBFCOMMISSIONING)
      DS_app_log_info("===%s [error cmd=%d] ===\n",
             get_status_str(RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_CMD_ERROR),
             packet[RBF_MAC_HEADER_INDEX_CMD]);
#endif
      return RBF_RET_STATUS_CMD_ERROR;
  }

  //1.hdr parse
#if defined (CONFIG_LOG_RBFCOMMISSIONING)
  DS_app_log_info("===[get]node join confirm header parse:===\n");
#endif
  RBF_header_parse(packet,RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX, &hdr);

  //2.payload
  //RBFMacMessageNodeJoinConfirm_t msg;
  RBF_node_join_confirm_payload_packetToMsg(packet,msg);




#if defined (CONFIG_DBG_CHECK_PRO)
  DS_app_log_info("===node join confirm parse:===\n");

  //比对数据
  //1.比对header
  if( memcmp(&hdr,&set_header,sizeof(RBFMacHeader_t)) == 0){
      DS_app_log_info("===%s===\n",get_status_str(RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_HDR_PARSE_TRUE));
  }
  else {
      DS_app_log_info("===%s===\n",get_status_str(RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_HDR_PARSE_FALSE));
      DS_app_log_hexdump_error("[set]:",(uint8_t *)&set_header,sizeof(RBFMacHeader_t));
      DS_app_log_hexdump_error("[get]:",(uint8_t *)&hdr,sizeof(RBFMacHeader_t));



  }
  //2.比对payload
  if(memcmp(&set_msg_join_confirm,&msg,sizeof(RBFMacMessageNodeJoinConfirm_t)-4) == 0){
      DS_app_log_info("===%s===\n",get_status_str(RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_PYD_PARSE_TRUE));
  }
  else {
      DS_app_log_info("===%s===\n",get_status_str(RBF_COMMSISSIONING_NODE_JOIN_CONFIRM_PYD_PARSE_TRUE));
      RBF_node_join_confirm_payload_log(set_msg_join_confirm);
      RBF_node_join_confirm_payload_log(msg);

  }
#endif

  return RBF_RET_STATUS_SUCCESS;
}
void RBF_node_join_confirm (void)
{
  RBFMacHeader_t hdr;
  uint8_t packet[25]={0};

  hdr.rollungCode = 0x12345678;    //4Bytes
  hdr.frameCtl = RBF_VERSION;       //1Byte


  hdr.syncClock  = 0xaabb;      //2Bytes
  hdr.hsclock = 0xcc;        //1Byte
  hdr.panId = RBF_DEFAULT_PANID;          //3Bytes
  hdr.srcId = RBF_DEFAULT_SRCID;          //1Byte
  hdr.destID = RBF_DEFAULT_DESTID;         //1Byte
  hdr.routID = RBF_DEFAULT_ROUTEID;         //1Byte
  hdr.cmd = RBF_CMD_NODE_PAGING;            //1Byte

  //memset(packet,0,sizeof(packet));
  RBF_packet_build_header(hdr,packet);

#if defined (CONFIG_LOG_RBFCOMMISSIONING)
  DS_app_log_hexdump_debug("node paging header: ",packet+RBF_MAC_HEADER_INDEX_FIRST,RBF_HEADER_LENGTH_MAX);
#endif

}



